configremote: Support --json and --json-error-messages
authorJoey Hess <joeyh@joeyh.name>
Wed, 10 May 2023 18:19:32 +0000 (14:19 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 10 May 2023 18:21:42 +0000 (14:21 -0400)
Seems unlikely to be too useful, but who knows.

Moved the checkSafeConfig call to happen after an action is started, so
it will be captured by --json-error-messages

Sponsored-By: the NIH-funded NICEMAN (ReproNim TR&D3) project
CHANGELOG
Command/ConfigRemote.hs
doc/todo/--json_for_unannex__and_ideally_any_other_command_.mdwn

index 2c3fc9a8f4370edfeb2f8b20acef257f0105d5e6..7628b5733d745aae623735e8f58c431c89ea9654 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -38,10 +38,10 @@ git-annex (10.20230408) UNRELEASED; urgency=medium
   * initremote: Avoid creating a remote that is not encrypted when gpg is
     broken.
   * Support --json and --json-error-messages in many more commands
-    (addunused, dead, describe, dropunused, enableremote, expire, fix,
-    importfeed, init, initremote, log, merge, migrate, reinit, reinject,
-    rekey, renameremote, rmurl, semitrust, setpresentkey, trust, unannex,
-    undo, untrust, unused, upgrade)
+    (addunused, configremote, dead, describe, dropunused, enableremote,
+    expire, fix, importfeed, init, initremote, log, merge, migrate, reinit,
+    reinject, rekey, renameremote, rmurl, semitrust, setpresentkey, trust,
+    unannex, undo, untrust, unused, upgrade)
   * log: When --raw-date is used, display only seconds from the epoch, as
     documented, omitting a trailing "s" that was included in the output
     before.
index 862fcb2cb82438505dc0082fab5218ed3555e083..4bed32998f40b7ad4fb7d4499eba711dd3adcfe4 100644 (file)
@@ -20,41 +20,43 @@ import Command.EnableRemote (unknownNameError, startSpecialRemote', PerformSpeci
 import qualified Data.Map as M
 
 cmd :: Command
-cmd = command "configremote" SectionSetup
-       "changes special remote configuration"
-       (paramPair paramName $ paramOptional $ paramRepeating paramParamValue)
-       (withParams seek)
+cmd = withAnnexOptions [jsonOptions] $
+       command "configremote" SectionSetup
+               "changes special remote configuration"
+               (paramPair paramName $ paramOptional $ paramRepeating paramParamValue)
+               (withParams seek)
 
 seek :: CmdParams -> CommandSeek
 seek = withWords (commandAction . start)
 
 start :: [String] -> CommandStart
 start [] = unknownNameError "Specify the remote to configure."
-start (name:rest) = do
-       cfg <- safeConfig rest
-       deadLast name $ startSpecialRemote name cfg
+start (name:inputconfig) = deadLast name $
+       startSpecialRemote (checkSafeConfig inputconfig) name
+               (Logs.Remote.keyValToConfig Proposed inputconfig)
 
 {- Since this command stores config without calling the remote's setup
  - method to validate it, it can only be used on fields that are known to
  - be safe to change in all remotes. -}
-safeConfig :: [String] -> Annex Remote.RemoteConfig
-safeConfig cs = do
+checkSafeConfig :: [String] -> Annex ()
+checkSafeConfig cs = do
        let rc = Logs.Remote.keyValToConfig Proposed cs
        forM_ (M.keys rc) $ \k ->
                when (fromProposedAccepted k `notElem` safefields) $
                        giveup $ "Cannot change field \"" ++ fromProposedAccepted k  ++ "\" with this command. Use git-annex enableremote instead."
        case SpecialRemote.parseRemoteConfig rc (Remote.RemoteConfigParser ps Nothing) of
                Left err -> giveup err
-               Right _ -> return rc
+               Right _ -> return ()
   where
        ps = [ SpecialRemote.autoEnableFieldParser ]
        safefields = [ fromProposedAccepted SpecialRemote.autoEnableField ]
 
-startSpecialRemote :: Git.RemoteName -> Remote.RemoteConfig -> [(UUID, Remote.RemoteConfig, Maybe (SpecialRemote.ConfigFrom UUID))] -> CommandStart
-startSpecialRemote = startSpecialRemote' "configremote" performSpecialRemote
+startSpecialRemote :: Annex () -> Git.RemoteName -> Remote.RemoteConfig -> [(UUID, Remote.RemoteConfig, Maybe (SpecialRemote.ConfigFrom UUID))] -> CommandStart
+startSpecialRemote = startSpecialRemote' "configremote" performSpecialRemote
 
-performSpecialRemote :: PerformSpecialRemote
-performSpecialRemote _ u _ c _ mcu = do
+performSpecialRemote :: Annex () -> PerformSpecialRemote
+performSpecialRemote precheck _ u _ c _ mcu = do
+       precheck
        case mcu of
                Nothing -> Logs.Remote.configSet u c
                Just (SpecialRemote.ConfigFrom cu) ->
index 6b748c1146500f9f2460868d8bb778cbd364692c..69001985925f1b93237616a17ef02460f002532b 100644 (file)
@@ -41,9 +41,6 @@ These commands have been updated to support --json:
 * git-annex-upgrade
 * git-annex-initremote
 * git-annex-enableremote
-
-Provisional list of commands that don't support --json and maybe should:
-
 * git-annex-configremote
 
 These commands could support json, but I punted:
@@ -57,6 +54,8 @@ These commands could support json, but I punted:
 
 These commands have been reviewed and should not support json:
 
+(Discussion welcome of course if you disagree..)
+
 * git-annex-contentlocation, git-annex-lookupkey, git-annex-calckey,
   (plumbing, output already machine parseable)
 * git-annex-setkey (plumbing)